﻿using B.S.Ingredients.APIRead.Application.Command.ImmunityCommand;
using B.S.Ingredients.Domain;
using B.S.Ingredients.ErrorCode;
using B.S.Ingredients.Infartructrue.Interfaces;
using MediatR;

namespace B.S.Ingredients.APIRead.Application.CommandHandler.ImmunityCommandHandler
{
    public class ImmunitDeleteCommandHandler : IRequestHandler<ImmunitDeleteCommand, APIResult<int>>
    {
        public ImmunitDeleteCommandHandler(IBaseRepository<Immunity> immunitRepository)
        {
            ImmunitRepository = immunitRepository;
        }

        public IBaseRepository<Immunity> ImmunitRepository { get; }

        public  async Task<APIResult<int>> Handle(ImmunitDeleteCommand request, CancellationToken cancellationToken)
        {
            var path = ImmunitRepository.GetAll().FirstOrDefault(a => a.InformatId == request.Id);
            if (path == null)
            {
                return new APIResult<int>
                {
                    Code = ResultCode.error,
                    Mag = "未查询到该数据",
                    Data = 0
                };
            }
            else
            {
                path.isDel = true;
                var entity = await ImmunitRepository.Update(path);
                return new APIResult<int>
                {
                    Code = ResultCode.success,
                    Mag = "删除成功",
                    Data = entity
                };


            }
        }
        
        
    }
}
